const aedes = require('aedes')();
const server = require('net').createServer(aedes.handle);
const wsServer = require('ws').Server;
const httpServer = require('http').createServer();

// MQTT over TCP (default port 1883)
server.listen(1883, () => {
    console.log('MQTT server listening on port 1883');
});

// MQTT over WebSocket (port 8883)
const wss = new wsServer({ server: httpServer });
wss.on('connection', (ws) => {
    const stream = require('websocket-stream')(ws);
    aedes.handle(stream);
});

httpServer.listen(8883, () => {
    console.log('MQTT over WebSocket running on ws://localhost:8883');
});
// server.js (HTTP 部分)
httpServer.on('request', (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.end('MQTT WebSocket server');
});